#ifndef LOG_LOGBUFFER_H
#define LOG_LOGBUFFER_H

#include "utility/Noncopyable.h"
#include "utility/cast.h"

#include <algorithm>
#include <array>

template<std::size_t N>
class Logbuffer final
{
public:
    Logbuffer(): _data{}, _current{_data.begin( )} {};
    ~Logbuffer() = default;

    long available_length() const { return static_cast<long>(N) - length(); }
    char *current() const { return _current; }
    const char *data() const { return _data.data(); }
    long length() const { return _current - _data.begin(); }

    void add(std::size_t length) { _current += length; };
    void append(const char *buffer, std::size_t length);
    void bzero() { _data.fill(0); }
    void clear() { _current = _data.begin(); }
    void reset(std::size_t newCur) { _current = _data.begin() + newCur; }

private:
    NONCOPYABLE(Logbuffer);

    using _BufferType = std::array<char, N>;
    
    _BufferType _data;
    typename _BufferType::iterator _current;
};

template<std::size_t N>
void Logbuffer<N>::append(const char *buffer, std::size_t length) {
    if (Cast::GE(length, available_length())) {
        return;
    }

    std::copy_n(buffer, length, _current);
    add(length);
}

#endif